package com.cskaoyan.service.user.impl;

import com.cskaoyan.bean.common.BasePageInfo;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.bean.po.MarketFootprint;
import com.cskaoyan.bean.po.MarketFootprintExample;
import com.cskaoyan.mapper.MarketFootprintMapper;
import com.cskaoyan.service.user.UserFootprintService;
import com.cskaoyan.util.StringUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author jzp
 * @since 2022/11/20 17:39
 */
@Service
public class UserFootprintServiceImpl implements UserFootprintService {
    @Autowired
    MarketFootprintMapper marketFootprintMapper;

    @Override
    public CommonData list(BasePageInfo info, String userId, String goodsId) {
        PageHelper.startPage(info.getPage(), info.getLimit());
        MarketFootprintExample example = new MarketFootprintExample();
        MarketFootprintExample.Criteria criteria = example.createCriteria();
        example.setOrderByClause(info.getSort() + " " + info.getOrder());
        if (!StringUtil.isEmpty(userId)) {
            try {
                criteria.andUserIdEqualTo(Integer.valueOf(userId));
            } catch (Exception e) {
                return null;
            }

        }
        if (!StringUtil.isEmpty(goodsId)) {
            try {
                criteria.andGoodsIdEqualTo(Integer.valueOf(goodsId));
            } catch (Exception e) {
                return null;
            }

        }
        List<MarketFootprint> marketFootprintList = marketFootprintMapper.selectByExample(example);
        PageInfo<MarketFootprint> pageInfo = new PageInfo<>(marketFootprintList);

        return CommonData.data(pageInfo);
    }
}
